/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.kernel;

import org.openaecfm.model.kernel.TypeObject;
import org.openaecfm.model.kernel.TypeProcess;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TypeProcessIO implements
		IEntityInstanceUtilOutput<TypeProcess>,
		IEntityInstanceUtilInput<TypeProcess> {

	private IEntityInstanceUtilOutput<TypeObject> utilOutput;
	private IEntityInstanceUtilInput<TypeObject> utilInput;

	public void setUtilOutput(IEntityInstanceUtilOutput<TypeObject> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(IEntityInstanceUtilInput<TypeObject> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, TypeProcess pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identification
		Parameter identification = sei.getParameter(6);
		if (identification.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) identification;
			pojo.setIdentification(s.toUTF8());
		}

		// Set LongDescription
		Parameter longDescription = sei.getParameter(7);
		if (longDescription.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) longDescription;
			pojo.setLongDescription(s.toUTF8());
		}

		// Set ProcessType
		Parameter processType = sei.getParameter(8);
		if (processType.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) processType;
			pojo.setProcessType(s.toUTF8());
		}
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei, TypeProcess pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	@Override
	public void exportSPF(TypeProcess pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Identification
		if (pojo.getIdentification() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getIdentification(),
					StringParameter.UTF_8));
		}

		// Set LongDescription
		if (pojo.getLongDescription() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getLongDescription(),
					StringParameter.UTF_8));
		}

		// Set ProcessType
		if (pojo.getProcessType() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getProcessType(),
					StringParameter.UTF_8));
		}
	}
}
